package com.bytz.modules.cms.product.service.impl;

import com.bytz.common.exception.BytzBootException;
import com.bytz.common.system.base.service.impl.AbstractCrudService;
import com.bytz.modules.cms.product.entity.ProductDiscountCategory;
import com.bytz.modules.cms.product.mapper.ProductDiscountCategoryMapper;
import com.bytz.modules.cms.product.model.ProductDiscountCategoryDetail;
import com.bytz.modules.cms.product.service.IAdminProductDiscountCategoryService;
import com.bytz.modules.cms.product.service.IProductDiscountCategoryLevelService;
import com.bytz.modules.cms.productLine.entity.ProductLine;
import com.bytz.modules.cms.productLine.service.IProductLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: cms-backend
 * @BelongsPackage: com.bytz.modules.cms.product.service.impl
 * @Author: dst
 * @CreateTime: 2022-10-27  16:03
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class AdminProductDiscountCategoryServiceImpl extends AbstractCrudService<ProductDiscountCategoryMapper, ProductDiscountCategory, ProductDiscountCategory> implements IAdminProductDiscountCategoryService {

    @Autowired
    private IProductLineService productLineService;

    @Autowired
    private IProductDiscountCategoryLevelService discountCategoryLevelService;

    @Override
    protected ProductDiscountCategory insertSaveObject(ProductDiscountCategory saveRo) {
        if (lambdaQuery()
                .eq(ProductDiscountCategory::getProductLineId, saveRo.getProductLineId())
                .eq(ProductDiscountCategory::getCategoryName, saveRo.getCategoryName())
                .exists()) {
            throw new BytzBootException("已存在同名的分类，无法添加");
        }
        save(saveRo);
        return saveRo;
    }

    @Override
    protected ProductDiscountCategory updateSaveObject(ProductDiscountCategory saveRo) {
        if (lambdaQuery()
                .eq(ProductDiscountCategory::getProductLineId, saveRo.getProductLineId())
                .eq(ProductDiscountCategory::getCategoryName, saveRo.getCategoryName())
                .ne(ProductDiscountCategory::getId, saveRo.getId())
                .exists()) {
            throw new BytzBootException("已存在同名的分类，无法添加");
        }
        updateById(saveRo);
        return saveRo;
    }

    @Override
    protected ProductDiscountCategory saveOthers(ProductDiscountCategory saveRo) {
        return null;
    }

    @Override
    protected boolean deleteEntity(ProductDiscountCategory entity) {

        return removeById(entity.getId());
    }

    @Override
    protected void deleteOthers(ProductDiscountCategory entity) {

    }

    @Override
    public List<ProductDiscountCategory> getByProductLine(String productLineId) {
        return lambdaQuery().eq(ProductDiscountCategory::getProductLineId, productLineId).list();
    }

    @Override
    public List<ProductDiscountCategory> getByLoginUser() {
        String lineId = productLineService.getLoginUserLine().map(ProductLine::getId).orElse(null);
        return lambdaQuery()
                .eq(lineId != null, ProductDiscountCategory::getProductLineId, lineId)
                .list();
    }

    @Override
    public List<ProductDiscountCategoryDetail> getDiscountByLevel(String id) {
        return discountCategoryLevelService.getByProductLineLevel(id);
    }
}
